//全局变量
var layer=null;

//监听事件
layui.use(['form','layer'], function(){
    var form = layui.form;
    layer = layui.layer;

    loadData();
    //监听提交
    form.on('submit(add)', function(data){
        formAddSubmit(data);
        return false;
    });
});

//加载数据
function loadData() {
    var id=GetQueryString("id");
    var url =  "/admin/{subFolder}/{tblName}View";
    $.ajax({
            type: "post",
            url: url,
            data: {
                id:id
            },
            dataType: "json",
            error: function (request) {
                ajaxError(request, $(this));
            },
            success: function (result) {
                console.log(result);
                ajaxSuccess(result, $(this));
                var code=result.code;
                if(code==0){
                    loadJson(result.data);
                }else{
                    layer.alert("获取失败："+result.msg);
                }
            }
        }
    );

}

//提交表单
function formAddSubmit(data) {
    console.log(data.field);
    var formData=data.field;
    var url="/admin/{subFolder}/{tblName}add"
    $.ajax({
            type: "post",
            url: url,
            data: formData,
            dataType: "json",
            error: function (request) {
                ajaxError(request, $(this));
            },
            success: function (result) {
                ajaxSuccess(result, $(this));
                var code=result.code;
                if(code==0){
                    layer.alert("提交成功", {icon: 6}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.loadData();
                        parent.layer.close(index);
                    });

                }else{
                    layer.alert("提交失败:"+result.msg );

                }
            }
        }
    );
}
